package com.shrct.businessprocess.product.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.product.RCProductPurchaseInfo;
import com.shrct.businessprocess.product.RCProductPurchaseInfoCriteria;
import com.shrct.businessprocess.product.RCProductSpecification;
import edu.fudan.mylang.pf.query.HQLBasedPagedQueryCriteria;
import java.util.Date;
import org.hibernate.Query;

public class RCProductPurchaseInfoCriteriaImpl extends HQLBasedPagedQueryCriteria<RCProductPurchaseInfo> implements RCProductPurchaseInfoCriteria {
  private RCEnterprise enterprise;
  
  public RCEnterprise getEnterprise() {
    return this.enterprise;
  }
  
  public RCProductPurchaseInfoCriteria setEnterprise(final RCEnterprise enterprise) {
    this.enterprise = enterprise;
    return this;			
    
  }
  
  private RCProductSpecification productSpec;
  
  public RCProductSpecification getProductSpec() {
    return this.productSpec;
  }
  
  public RCProductPurchaseInfoCriteria setProductSpec(final RCProductSpecification productSpec) {
    this.productSpec = productSpec;
    return this;			
    
  }
  
  private Date inStockDateFrom;
  
  public Date getInStockDateFrom() {
    return this.inStockDateFrom;
  }
  
  public RCProductPurchaseInfoCriteria setInStockDateFrom(final Date inStockDateFrom) {
    this.inStockDateFrom = inStockDateFrom;
    return this;			
    
  }
  
  private Date inStockDateTo;
  
  public Date getInStockDateTo() {
    return this.inStockDateTo;
  }
  
  public RCProductPurchaseInfoCriteria setInStockDateTo(final Date inStockDateTo) {
    this.inStockDateTo = inStockDateTo;
    return this;			
    
  }
  
  private Date deliverDateFrom;
  
  public Date getDeliverDateFrom() {
    return this.deliverDateFrom;
  }
  
  public RCProductPurchaseInfoCriteria setDeliverDateFrom(final Date deliverDateFrom) {
    this.deliverDateFrom = deliverDateFrom;
    return this;			
    
  }
  
  private Date deliverDateTo;
  
  public Date getDeliverDateTo() {
    return this.deliverDateTo;
  }
  
  public RCProductPurchaseInfoCriteria setDeliverDateTo(final Date deliverDateTo) {
    this.deliverDateTo = deliverDateTo;
    return this;			
    
  }
  
  private Boolean ready4DeliverOnly;
  
  public Boolean getReady4DeliverOnly() {
    return this.ready4DeliverOnly;
  }
  
  public RCProductPurchaseInfoCriteria setReady4DeliverOnly(final Boolean ready4DeliverOnly) {
    this.ready4DeliverOnly = ready4DeliverOnly;
    return this;			
    
  }
  
  public void postInitialize() {
    boolean _equals = Objects.equal(this.ready4DeliverOnly, null);
    if (_equals) {
      this.ready4DeliverOnly = Boolean.valueOf(false);
    }
  }
  
  protected String buildHQLFrom() {
    return " from com.shrct.businessprocess.product.impl.RCProductPurchaseInfoImpl this ";
  }
  
  protected String buildHQLBody() {
    String hql = "where 1 = 1 ";
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getProductSpec()) ){
    	hql += " and ((this.orderItem.productSpec) = (:productSpec))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getInStockDateFrom()) ){
    	hql += " and ((this.inStockDate) >= (:inStockDateFrom))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getInStockDateTo()) ){
    	hql += " and ((this.inStockDate) <= (:inStockDateTo))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getDeliverDateFrom()) ){
    	hql += " and ((this.deliverDate) >= (:deliverDateFrom))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getDeliverDateTo()) ){
    	hql += " and ((this.deliverDate) <= (:deliverDateTo))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getReady4DeliverOnly()) ){
    	hql += " and (((:ready4DeliverOnly) = (false)) or ((this.deliverQuantity) < (this.inStockQuantity)))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getEnterprise()) ){
    	hql += " and (:enterprise  IN (select d from this.deliverTargets as u left join  u.deliverTarget as d   where  u.active = 1      ))" ;
    }
    return hql;
    
  }
  
  public void initOrderBys() {
    
  }
  
  protected void fillParameter(final Query query) {
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getEnterprise())){
    	query.setParameter("enterprise",getEnterprise()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getProductSpec())){
    	query.setParameter("productSpec",getProductSpec()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getInStockDateFrom())){
    	query.setParameter("inStockDateFrom",getInStockDateFrom()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getInStockDateTo())){
    	query.setParameter("inStockDateTo",getInStockDateTo()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getDeliverDateFrom())){
    	query.setParameter("deliverDateFrom",getDeliverDateFrom()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getDeliverDateTo())){
    	query.setParameter("deliverDateTo",getDeliverDateTo()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getReady4DeliverOnly())){
    	query.setParameter("ready4DeliverOnly",getReady4DeliverOnly()) ;
    }			
    
  }
  
  protected Boolean isDistinct() {
    return false;
  }
  
  public RCProductPurchaseInfoCriteriaImpl() {
    initOrderBys();
  }
}
